﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoteServer.Member;

namespace VoteServer
{
    /// <summary>
    /// 
    /// 数据只能获得不能更改
    /// 更改数据只能通过数据层提供的对应服务对象中的方法
    /// </summary>
    /// 按照

    public class VoteControl//控制---可以由此类直接获得下属服务
    {

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Enroll(string accountNumber, string password)
        {
            VoteData voteData = new VoteData();//数据类 只能得到数据,更改需要调用该层所提供的服务对象的方法
            User user;//只能通过user服务进行更改
            DataServer dataServer = new DataServer();

            user = new User
            {
                 AccountName = accountNumber,
                 Password = password
            };

            foreach (User people in voteData.GetUserList())
            {
                if (people.AccountName == accountNumber)
                {
                    return false;//已经存在
                }
            }
            user.ID = voteData.GetUserList()[voteData.GetUserList().Count - 1].ID + 1;//设置Id
            if (dataServer.IsAddUser(user))
            {
            }else
            {
                throw new Exception();
            }
           
            return true;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string accountNumber, string password)
        {
            VoteData voteData=new VoteData();//数据类 只能得到数据,更改需要调用该层所提供的服务对象的方法
            User user;//只能通过user服务进行更改

            foreach (User people in voteData.GetUserList())
            {
                if (people.AccountName == accountNumber)//判断账号
                {
                    if (people.Password == password)//判断密码
                    {
                        user = new User
                        {
                            AccountName = accountNumber,
                            Password = password
                        };
                        return true;//名称，密码匹配成功
                    }
                    else
                    {
                        return false;//密码错误
                    }
                }
            }
            return false;//没有注册
        }
    }
}

///结构

//express   - voteserver    -enroll
//                      -Regis
//                     -getQuestion（question-option）    <-votedata-questionlist
//                     -userserver                                  -userlist
//                     -dataserver-update<-fileupdate
//                     
//                   -userlist



/* public VoteData voteData { get; private set; }//数据类 只能得到数据,更改需要调用该层所提供的服务对象的方法
public UserServer userServer { get; set; }//服务类<---功能类
public DataServer dataServer { get; set; }//服务类  需要voteData支持
public User user { get; private set; }//只能通过user服务进行更改*/
